//
//  openVIdeoPage.m
//  PLTest
//
//  Created by beeway10 on 14-3-5.
//  Copyright (c) 2014年 beeway10. All rights reserved.
//

#import "openVIdeoPage.h"

@interface openVIdeoPage ()

@end

@implementation openVIdeoPage
@synthesize isCallbacked,delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


-(id)initWithDefault{
    NSString*videoPath = [[NSBundle mainBundle]pathForResource:@"QDSP" ofType:@"mp4"];
    NSURL *contentURL = [NSURL fileURLWithPath:videoPath];//online 这儿设定在线视频地址
    //NSURL *contentURL = [[NSBundle mainBundle] URLForResource:@"opening" withExtension:@"mp4"];//local 本地视频地址
    self = [super initWithContentURL:contentURL];
    if (self) {
        // 注册通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackStateDidChange:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadStateDidChange:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(playbackFinished:) name: MPMoviePlayerPlaybackDidFinishNotification object :nil];
        
        isCallbacked = FALSE;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
    self.moviePlayer.controlStyle = MPMovieControlStyleNone;
    [self.moviePlayer prepareToPlay];
    [self.moviePlayer play];
    
    UIView *tapView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 1024)];
    tapView.backgroundColor = [UIColor clearColor];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTap:)];
    [tapView addGestureRecognizer:tap];
    [self.view addSubview:tapView];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
   
}

-(void)viewTap:(id)sender{
    //NSLog(@"-----viewTap: %d",self.moviePlayer.playbackState);
    [self popOpenVideo];
    
}

-(void)playbackStateDidChange:(NSNotification *)sender{
    NSLog(@"-----playbackStateDidChange: %d",self.moviePlayer.playbackState);
    if (self.moviePlayer.playbackState == MPMoviePlaybackStateStopped ) {
        [self popOpenVideo];
    }
}

- (void)playbackFinished:(NSNotification*)notification
{
    
    
    NSNumber* reason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
    switch ([reason intValue]) {
        case MPMovieFinishReasonPlaybackEnded:
            NSLog(@"-----Playback Ended");
            break;
        case MPMovieFinishReasonPlaybackError:
            NSLog(@"-----Playback Error");
            break;
        case MPMovieFinishReasonUserExited:
            NSLog(@"-----User Exited");
            break;
        default:
            break;
    }
    NSLog(@"-----playbackFinished: %d",[reason intValue]);
    
    
    [self popOpenVideo];
}

-(void)loadStateDidChange:(NSNotification *)sender{
    //NSLog(@"-----loadStateDidChange: %d",self.moviePlayer.loadState);
    if(self.moviePlayer.loadState == MPMovieLoadStatePlayable){
       
    }
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
    //return YES;
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:nil];

    //NSLog(@"--------open video controller delloc");
}

-(void)popOpenVideo
{
    @synchronized(self){
        if (!isCallbacked) {
            if (delegate && [delegate respondsToSelector:@selector(openVideoControllerDidStop)]) {
                isCallbacked = TRUE;
                [delegate openVideoControllerDidStop];
            }
        }
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
